#!/usr/bin/env pwsh

# Node.js Server Script (Start/Stop)
# Save original working directory
$OriginalDir = Get-Location

# Get command line argument, default to start mode
$mode = "start"
if ($args.Count -gt 0) {
    $mode = $args[0].ToLower()
}

# Function to check and start dependencies if needed
function Check-And-Start-Dependencies {
    # Check if MySQL is running
    if (-not (docker ps | Select-String -Pattern "mysql")) {
        Write-Host "MySQL is not running. Starting MySQL..." -ForegroundColor Yellow
        & "$PSScriptRoot\01manage_mysql.ps1"
    }
    
    # Check if Redis is running
    if (-not (docker ps | Select-String -Pattern "redis")) {
        Write-Host "Redis is not running. Starting Redis..." -ForegroundColor Yellow
        & "$PSScriptRoot\02manage_redis.ps1"
    }
}

if ($mode -eq "start") {
    Write-Host "Starting Node.js Server..." -ForegroundColor Yellow
    
    # Check and start dependencies
    Check-And-Start-Dependencies

    # 检查Docker网络是否存在
    Write-Host "Checking if server_network exists..." -ForegroundColor Yellow
    try {
        $networkExists = docker network ls | Select-String -Pattern "server_network"
        if (-not $networkExists) {
            Write-Host "Creating server_network..." -ForegroundColor Yellow
            docker network create server_network
        } else {
            Write-Host "server_network already exists." -ForegroundColor Green
        }
    } catch {
        Write-Host "Error checking network: $_" -ForegroundColor Red
        # 恢复原始工作目录后退出
        Set-Location $OriginalDir
        Exit 1
    }

    # Start Node.js server container without rebuilding
    Write-Host "Starting Node.js server container..." -ForegroundColor Yellow
    try {
        docker-compose -f "$PSScriptRoot\..\server\3-docker-compose-node-server-prod.yml" up -d
        
        # 验证容器是否成功启动
        Start-Sleep -Seconds 2
        $containerRunning = docker ps | Select-String -Pattern "node-server"
        if ($containerRunning) {
            Write-Host "Node.js Server started successfully!" -ForegroundColor Green
            Write-Host "You can check status with: docker-compose -f .\server\3-docker-compose-node-server-prod.yml ps" -ForegroundColor White
            Write-Host "To stop the service, run: ./03manage_node-server-prod.ps1 stop" -ForegroundColor White
        } else {
            Write-Host "Warning: Node.js server container may not be running properly." -ForegroundColor Red
            Write-Host "Please check logs with: docker logs node-server" -ForegroundColor Yellow
        }
    } catch {
        Write-Host "Error starting Node.js server: $_" -ForegroundColor Red
        Write-Host "Please check the error message above." -ForegroundColor Yellow
        # 恢复原始工作目录后退出
        Set-Location $OriginalDir
        Exit 1
    }
} elseif ($mode -eq "stop") {
    Write-Host "Stopping Node.js Server..." -ForegroundColor Yellow
    try {
        # Stop Node.js server container using docker-compose stop
        docker-compose -f "$PSScriptRoot\..\server\3-docker-compose-node-server-prod.yml" stop
        Write-Host "Node.js Server stopped successfully!" -ForegroundColor Green
        Write-Host "To start the service again, run: ./03start_node-server-prod.ps1 start" -ForegroundColor White
    } catch {
        Write-Host "Error stopping Node.js server: $_" -ForegroundColor Red
        # 恢复原始工作目录后退出
        Set-Location $OriginalDir
        Exit 1
    }
} else {
        Write-Host "Invalid mode. Please use 'start' or 'stop'." -ForegroundColor Red
        Write-Host "Example: ./03manage_node-server-prod.ps1 start" -ForegroundColor White
        Write-Host "Example: ./03manage_node-server-prod.ps1 stop" -ForegroundColor White
    # 恢复原始工作目录后退出
    Set-Location $OriginalDir
    Exit 1
}

# Return to original working directory
Set-Location $OriginalDir
Write-Host "Returned to original directory: $OriginalDir" -ForegroundColor White